<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=trim($_RQ['title']);
		$return['params']['status']=$_RQ['status'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_kaoshi_card',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$isdel=$_SESSION['isfounder']?true:false;
	$return['groupname']=$group[$groupid]['title'];
	$return['list']=$list?$list:array();
	$return['group']=$group?$group:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'答题卡列表'),
			array('op'=>'edit','title'=>'添加答题卡'),
		),
		'list'=>array('data'=>'list','del_one'=>$isdel,'delall'=>$isdel,'list'=>array(
			//array('name'=>'title','title'=>'答题卡标题','type'=>'string'),
			array('name'=>'member','title'=>'领取人','type'=>'member'),
			array('name'=>'codenumber','title'=>'卡号','type'=>'string'),
			array('name'=>'codepassword','title'=>'密码','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'card_status'),
		),'action'=>array(
			array('type'=>'href','action'=>'/card/bangka/','title'=>'绑卡','show'=>array('status'=>0)),
			array('type'=>'modal','op'=>'info','title'=>'查看绑卡人'),
		)),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'card_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['tiku_zu']=pdo_getall('jy_kaoshi_card',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['card_status']=$this->card_status;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_kaoshi_card',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_kaoshi_card',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='info'){
	$return=array();
	$id=intval($_RQ['id']);
	$card=pdo_get('jy_kaoshi_card',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item=pdo_get('jy_kaoshi_member',array('mid'=>$card['mid'],'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'code','title'=>'身份证','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'danwei','title'=>'单位','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$item=array('num'=>5);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回答题卡列表'),
		),
		'edit'=>array(
			array('name'=>'num','title'=>'一键答题卡数量','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['num'])){
		exi('请填写一键答题卡数量！','error');
	}
	$daysnum=date('Ymd',time());
	$count=pdo_count('jy_kaoshi_card',array('uniacid'=>$_SESSION['uniacid']),'id');
	for($i=1;$i<=$postdata['num'];$i++){
		$codenumber='NO'.$daysnum.sprintf("%04d", $i+$count);
		$codepassword=rand(100000,999999);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'codenumber'=>$codenumber,
			'codepassword'=>$codepassword,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_kaoshi_card',$data);
		$id=pdo_insertid();
	}
	exi('添加答题卡成功！','','list');
}
if($_RQ['op']=='bangka'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(empty($postdata['mid'])){
			exi('请选择会员！','error');
		}
		$zu_id=$postdata['zu_id'];
		if(empty($zu_id)){
			exi('请选择题库！','error');
		}
		$id=intval($postdata['id']);
		$card=pdo_get('jy_kaoshi_card',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if(!$card['id']){
			exi('卡不存在，绑卡失败！','error');
		}
		if($card['status'] || $card['mid']){
			exi('该答题卡已被领取！','error');
		}
		pdo_update('jy_kaoshi_card',array('mid'=>$postdata['mid'],'zu_id'=>$zu_id,'status'=>2),array('id'=>$card['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('答题卡绑定题库成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=array('id'=>$id);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回答题卡列表'),
		),
		'edit'=>array(
			array('name'=>'mid','title'=>'选择会员','type'=>'choose','action'=>'core/member.get_member/'),
			array('name'=>'zu_id','title'=>'选择题库','type'=>'select','options'=>'alltizu'),
		)
	);
	$return['alltizu']=pdo_getall('jy_kaoshi_tiku_zu',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');

	$return['htmls']=$htmls;
	exi($return);
}